package mock.service;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import ita.mock.dao.IUserDao;
import ita.mock.entity.User;
import ita.mock.service.impl.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

public class UserDaoImplTest2 {

	private UserServiceImpl service;
	private IUserDao dao;
	@Before
	public void setUp(){
		service=new UserServiceImpl();
		dao=EasyMock.mock(IUserDao.class);//mock模式
		service.setDao(dao);
	}
	@Test
	public void testQueryAll() {
		EasyMock.expect(dao.queryAll()).andReturn(new ArrayList<User>()).times(1);
		EasyMock.replay(dao);
		List<User> l1=service.showAll();
		EasyMock.verify(dao);
		assertThat(l1.size(),is(0));
		
		EasyMock.reset(dao);
		List<User> list=new ArrayList<User>();
		list.add(new User("123","456"));
		EasyMock.expect(dao.queryAll()).andReturn(list).times(1);
		EasyMock.replay(dao);
		List<User> l2=service.showAll();
		EasyMock.verify(dao);
		assertThat(l2.size(),is(1));
		
//		List<User> list=new ArrayList<User>();
//		list.add(new User("123","456"));
//		list.add(new User("111","111"));
//		dao.queryAll();
//		EasyMock.expectLastCall().andReturn(list).times(1);
//		EasyMock.replay(dao);
//		List<User> l1=service.showAll();
//		List<User> l2=service.showAll();
//		assertThat(l1.size(),is(0));
//		assertThat(l2.size(),is(2));
//		EasyMock.verify(dao);
	}

}
